﻿using AutoMapper;
using DailyApp.WebAPI.ApiResponses;
using DailyApp.WebAPI.DataModel;
using DailyApp.WebAPI.DTOs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace DailyApp.WebAPI.Controllers
{
    /// <summary>
    /// 备忘录DTO
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemoController : ControllerBase
    {
        //服务
        private readonly DailyAppDbContext db;

        private readonly IMapper mapper;


        /// <summary>
        /// 构造函数
        /// </summary>
        public MemoController(DailyAppDbContext _db, IMapper _mapper)
        {
            db = _db;
            mapper = _mapper;
        }


        /// <summary>
        /// 统计备忘录的数量信息
        /// </summary>
        /// <returns>1--统计成功 -99--异常</returns>
        [HttpGet]
        public IActionResult StatMemo()
        {
            ApiResponse res = new ApiResponse();

            try
            {
                //取出所有记录
                var listAll = db.MemoInfo.ToList();
                var listFinshed = listAll.Where(t => t.Status == 1).ToList();

                //组装DTO
                StatMemoDTO statMemoDTO = new StatMemoDTO
                {
                    TotalCount = listAll.Count,
                    FinshedCount = listFinshed.Count,
                };

                res.ResultCode = 1;
                res.ResultMsg = "统计待办事项成功";
                res.ResultData = statMemoDTO;
            }
            catch (Exception)
            {

                res.ResultCode = -99;
                res.ResultMsg = "服务器繁忙...";
            }

            return Ok(res);
        }


        /// <summary>
        /// 添加备忘录信息
        /// </summary>
        /// <param name="memoInfoDTO">备忘录信息</param>
        /// <returns>1--成功 -1:失败 -99：异常</returns>
        [HttpPost]
        public IActionResult AddMemoInfo(MemoInfoDTO memoInfoDTO)
        {
            var resp = new ApiResponse();

            try
            {
                MemoInfo memoInfo = mapper.Map<MemoInfo>(memoInfoDTO);

                db.MemoInfo.Add(memoInfo);
                var res = db.SaveChanges();

                if (res == 1)
                {
                    resp.ResultCode = 1;
                    resp.ResultMsg = "添加成功";
                }
                else
                {
                    resp.ResultCode = -1;
                    resp.ResultMsg = "添加失败";
                }
            }
            catch (Exception)
            {

                resp.ResultCode = -99;
                resp.ResultMsg = "服务器忙...";
            }

            return Ok(resp);
        }


        /// <summary>
        /// 获取正在备忘的备忘录信息
        /// </summary>
        /// <returns>1--成功 -1--失败</returns>
        [HttpGet]
        public IActionResult GetMemoingInfo()
        {
            ApiResponse resp = new ApiResponse();

            try
            {
                //var list = db.MemoInfo.Where(t => t.Status == 0);---方法语句，简洁高效，过滤性差

                //查询语句，SQL式写法，过滤性强
                var list = from A in db.MemoInfo
                           where A.Status == 0
                           select new MemoInfoDTO
                           {
                               MemoId = A.MemoId,
                               Title = A.Title,
                               Content = A.Content,
                               Status = A.Status,
                           };

                resp.ResultCode = 1;
                resp.ResultMsg = "查询成功";
                resp.ResultData = list;
            }
            catch (Exception)
            {

                resp.ResultCode = -1;
                resp.ResultMsg = "查询失败";
            }

            return Ok(resp);
        }


        /// <summary>
        /// 条件查询待办事项信息
        /// </summary>
        /// <param name="title">标题(模糊查询)</param>
        /// <param name="status">状态(条件查询)</param>
        /// <returns>查询结果 1--成功 -1--失败</returns>
        [HttpGet]
        public IActionResult QueryMemoInfo(string? title, int? status)
        {
            var resp = new ApiResponse();

            try
            {
                var list = from A in db.MemoInfo
                           select new MemoInfo
                           {
                               MemoId = A.MemoId,
                               Title = A.Title,
                               Content = A.Content,
                               Status = A.Status
                           };

                //标题
                if (title != null && !string.IsNullOrEmpty(title))
                {
                    list = list.Where(t => t.Title.Contains(title));
                }

                //状态
                if (status == 1 || status == 0)

                {
                    list = list.Where(t => t.Status == status);
                }

                resp.ResultCode = 1;
                resp.ResultMsg = "Query Success!";
                resp.ResultData = list;

            }
            catch (Exception)
            {

                resp.ResultCode = -1;
                resp.ResultMsg = "Query Failed!";
            }


            return Ok(resp);
        }


        /// <summary>
        /// 修改备忘录事项的信息
        /// </summary>
        /// <param name="newWaitInfo 前端修改好的数据,但是Id是固定的"></param>
        /// <returns>1--修改成功 -1--失败  -99--异常</returns>
        [HttpPut]
        public IActionResult ModifyMemoInfo(MemoInfoDTO newMemoInfo)
        {
            var resp = new ApiResponse();

            try
            {
                var dbInfo = db.MemoInfo.Find(newMemoInfo.MemoId);//找到该数据
                if (dbInfo != null)
                {
                    mapper.Map(newMemoInfo, dbInfo);
                    var count = db.SaveChanges();

                    if (count == 1)
                    {
                        resp.ResultCode = 1;
                        resp.ResultMsg = "修改成功";
                    }
                    else
                    {
                        resp.ResultCode = -1;
                        resp.ResultMsg = "修改失败";
                    }
                }

            }
            catch (Exception)
            {

                resp.ResultCode = -99;
                resp.ResultMsg = "异常";
            }
            return Ok(resp);
        }


        /// <summary>
        /// 删除待办事项
        /// </summary>
        /// <param name="delMemoInfo">待删除的待办事项</param>
        /// <returns>1--成功 -1--失败 0--未查找到该数据 -99--异常</returns>
        [HttpDelete]
        public IActionResult DeleteMemoInfo(int delId)//delete请求一般不能传递整体的参数
        {
            var resp = new ApiResponse();

            try
            {
                //查找这条数据
                var delItem = db.MemoInfo.FirstOrDefault(t => t.MemoId == delId);

                if (delItem != null)
                {
                    db.MemoInfo.Remove(delItem);
                    var count = db.SaveChanges();

                    if (count == 1)
                    {
                        resp.ResultCode = 1;
                        resp.ResultMsg = "Delete Success!";
                    }
                    else
                    {
                        resp.ResultCode = -1;
                        resp.ResultMsg = "Delete Failed!";
                    }
                }

                else
                {
                    resp.ResultCode = 0;
                    resp.ResultMsg = "Research Failed!";
                }

            }
            catch (Exception)
            {

                resp.ResultCode = -99;
                resp.ResultMsg = "Error!";
            }



            return Ok(resp);
        }

    }
}
